package com.daocao.ds.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.adapter.HomeOrderAdapter;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.bean.DealListBean;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.view.OnLoadeMoreListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-11-16.
 */

public class HomeOrderFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    Unbinder unbinder;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.btn_turn_top)
    ImageView btnTurnTop;
    @BindView(R.id.layout_no_data)
    LinearLayout layoutNoData;
    @BindView(R.id.layout_has_data)
    LinearLayout layoutHasData;
    private HomeOrderAdapter adapter;
    private int rush_state;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getDealList(page);
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_home_order;
    }

    @Override
    protected void initView() {
        rush_state = getArguments().getInt("id", 0);
        list.addOnScrollListener(onLoadeMoreListener);

        adapter = new HomeOrderAdapter(getContext());
//        adapter.setOnChooseListener(new HomeOrderAdapter.OnChooseListener() {
//            @Override
//            public void onSuccess(String id) {
//
//            }
//        });
        list.setAdapter(adapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getDealList(1);
            }
        });
        onLoadeMoreListener.setPage();
        getDealList(1);
    }

    public void getDealList(int page) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.makeText(getContext(), "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);
        DealListBean bean = new DealListBean();
        bean.page = page;
        bean.rush_state = rush_state;
        Api.api_service.getDealList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if(adapter.getItemCount()==0){
                        layoutNoData.setVisibility(View.VISIBLE);
                        layoutHasData.setVisibility(View.GONE);
                    }else {
                        layoutNoData.setVisibility(View.GONE);
                        layoutHasData.setVisibility(View.VISIBLE);
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn_turn_top)
    public void onViewClicked() {
        list.scrollToPosition(0);
    }

}
